package com.ruoyi.web.controller.system;


import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.service.IExamItemService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: samsung
 * Date: 2019/9/14
 * Time: 16:11
 * Description:答题卡,控制器.
 */
@Controller
@RequestMapping("/exam/paper")
public class ExamPaperCardController extends BaseController {

    @Autowired
    private IExamItemService iExamItemService;

    private static final Logger LOGGER = LoggerFactory.getLogger(ExamPaperCardController.class);

    @RequestMapping("/chinese")
    public String startChineseExam() {
        return "paper/testChinesePaper.html";
    }

    @PostMapping("/chinese/list")
    @ResponseBody
    public TableDataInfo chineseList() {
        List<ExamItems> itemsByPaperId = iExamItemService.getItemsByPaperId(1);
        return getDataTable(itemsByPaperId,itemsByPaperId.size()+1);
    }

    // 提交答案，运算分数控制器。
    @RequestMapping("/chinese/result")
    @ResponseBody
    public AjaxResult examChineseResult(@RequestBody ExamResult data) {
        ExamResult examResult = iExamItemService.saveScore(data, 1, getSysUser().getUserId() );
        return success(examResult);
    }


    @RequestMapping("/math")
    public String startMathExam() {
        return "paper/testMathPaper.html";
    }
    @PostMapping("/math/list")
    @ResponseBody
    public TableDataInfo mathList() {
        List<ExamItems> itemsByPaperId = iExamItemService.getItemsByPaperId(2);
        return getDataTable(itemsByPaperId,itemsByPaperId.size()+1);
    }

    // 提交答案，运算分数控制器。
    @RequestMapping("/math/result")
    @ResponseBody
    public AjaxResult examMathResult(@RequestBody ExamResult data) {
        ExamResult examResult = iExamItemService.saveScore(data, 2, getSysUser().getUserId() );
        return success(examResult);
    }



    @RequestMapping("/english")
    public String startEnglishExam() {
        return "paper/testEnglishPaper.html";
    }
    @PostMapping("/english/list")
    @ResponseBody
    public TableDataInfo englishList() {
        List<ExamItems> itemsByPaperId = iExamItemService.getItemsByPaperId(3);
        return getDataTable(itemsByPaperId,itemsByPaperId.size()+1);
    }

    // 提交答案，运算分数控制器。
    @RequestMapping("/english/result")
    @ResponseBody
    public AjaxResult examEnglishResult(@RequestBody ExamResult data) {
        ExamResult examResult = iExamItemService.saveScore(data, 3, getSysUser().getUserId() );
        return success(examResult);
    }



    @RequestMapping("/computer")
    public String startComputerExam() {
        return "paper/testComputerPaper.html";
    }
    @PostMapping("/computer/list")
    @ResponseBody
    public TableDataInfo computerList() {
        List<ExamItems> itemsByPaperId = iExamItemService.getItemsByPaperId(4);
        return getDataTable(itemsByPaperId,itemsByPaperId.size()+1);
    }

    // 提交答案，运算分数控制器。
    @RequestMapping("/computer/result")
    @ResponseBody
    public AjaxResult englishComputerResult(@RequestBody ExamResult data) {
        ExamResult examResult = iExamItemService.saveScore(data, 4, getSysUser().getUserId() );
        return success(examResult);
    }



    @RequestMapping("/os")
    public String startOsExam() {
        return "paper/testOSPaper.html";
    }
    @PostMapping("/os/list")
    @ResponseBody
    public TableDataInfo osList() {
        List<ExamItems> itemsByPaperId = iExamItemService.getItemsByPaperId(5);
        return getDataTable(itemsByPaperId,itemsByPaperId.size()+1);
    }

    // 提交答案，运算分数控制器。
    @RequestMapping("/os/result")
    @ResponseBody
    public AjaxResult examOsResult(@RequestBody ExamResult data) {
        ExamResult examResult = iExamItemService.saveScore(data, 5, getSysUser().getUserId() );
        return success(examResult);
    }



    @RequestMapping("/ds")
    public String startDsExam() {
        return "paper/testDSPaper.html";
    }
    @PostMapping("/ds/list")
    @ResponseBody
    public TableDataInfo dsList() {
        List<ExamItems> itemsByPaperId = iExamItemService.getItemsByPaperId(6);
        return getDataTable(itemsByPaperId,itemsByPaperId.size()+1);
    }

    // 提交答案，运算分数控制器。
    @RequestMapping("/ds/result")
    @ResponseBody
    public AjaxResult examDsResult(@RequestBody ExamResult data) {
        ExamResult examResult = iExamItemService.saveScore(data, 6, getSysUser().getUserId() );
        return success(examResult);
    }



    @RequestMapping("/c")
    public String startCExam() {
        return "paper/testCPaper.html";
    }
    @PostMapping("/c/list")
    @ResponseBody
    public TableDataInfo cList() {
        List<ExamItems> itemsByPaperId = iExamItemService.getItemsByPaperId(7);
        return getDataTable(itemsByPaperId,itemsByPaperId.size()+1);
    }

    // 提交答案，运算分数控制器。
    @RequestMapping("/c/result")
    @ResponseBody
    public AjaxResult examCResult(@RequestBody ExamResult data) {
        ExamResult examResult = iExamItemService.saveScore(data, 7, getSysUser().getUserId() );
        return success(examResult);
    }




    // 查看答案解析；
    @RequestMapping("/{paperid}/check")
    public String getAngni(@PathVariable int paperid, String papername, Model model,
                           HttpSession session) {
        ExamUser online = (ExamUser) session.getAttribute("online");

        model.addAttribute("items", iExamItemService.getItemsAndScore(paperid, online.getUserid()));

        model.addAttribute("papername", papername);

        return "/paper/chechPaper";

    }

}
